/*
 * This file is part of Shopping Lists Organizer software
 *
 * Copyright (C) 2011 Denis Kvita <dkvita@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <QtGui>
#include <QtSql>

#include "listimportdlg.h"
#include "calendar.h"

listImportDlg::listImportDlg(QString listTitle, QDate listDate, QWidget *parent) :
	QDialog(parent)
{
	title= listTitle;
	date= listDate;
	setWindowTitle(tr("Import"));
	question= new QLabel(tr("The list with title <b>'")
						 +listTitle+tr("'</b> for  the date <b>'")
						 +listDate.toString("yyyy-MM-dd")
						 +tr("'</b> already exists! <br><br><b>What do you want to do</b>?"));
	answer= new QComboBox;
	answer->addItem(tr("Rename the new list"));
	answer->addItem(tr("Automatic merge the new and existing list"));
	//answer->addItem(tr("Show merge dialog"));
	connect(answer, SIGNAL(activated(int)), this, SLOT(onAnswer(int)));

	mergeQuestion= new QLabel(tr("if the new purchase title match the existing one: "));
	mergeDetail= new QComboBox;
	mergeDetail->addItem(tr("replace the existing purchase with new"));
	mergeDetail->addItem(tr("skip the new purchase"));
	mergeDetail->addItem(tr("add the new purchase and leave the old"));
	connect(mergeDetail, SIGNAL(activated(int)), this, SLOT(onMergeDetail(int)));
	mergeQuestion->hide();
	mergeDetail->hide();

	newTitleL= new QLabel(tr("Title: "));
	newTitle= new QLineEdit;
	newTitle->setText(listTitle);
	newDateL= new QLabel(tr("Date: "));
	newDate= new QDateEdit;
	newDate->setCalendarPopup(true);
	newDate->setCalendarWidget(new Calendar());
	if(QLocale::system().country() == QLocale::RussianFederation || QLocale::system().country() == QLocale::Ukraine) newDate->calendarWidget()->setFirstDayOfWeek(Qt::Monday);
	newDate->setDate(listDate);

	QHBoxLayout *titleLt= new QHBoxLayout;
	titleLt->addWidget(newTitleL);
	titleLt->addWidget(newTitle);
	titleLt->setMargin(0);

	QHBoxLayout *dateLt= new QHBoxLayout;
	dateLt->addWidget(newDateL);
	dateLt->addWidget(newDate);
	dateLt->addStretch(1);
	dateLt->setMargin(0);

	QPushButton *ok= new QPushButton(tr("Ok"));
	connect(ok, SIGNAL(clicked()), this, SLOT(accept()));

	QPushButton *cnl= new QPushButton(tr("Cancel"));
	connect(cnl, SIGNAL(clicked()), this, SLOT(reject()));

	QHBoxLayout *clt= new QHBoxLayout;
	clt->addStretch(1);
	clt->addWidget(ok);
	clt->addStretch(1);
	clt->addWidget(cnl);
	clt->addStretch(1);

	QVBoxLayout *mlt= new QVBoxLayout;
	mlt->addWidget(question);
	mlt->addWidget(answer);
	mlt->addWidget(mergeQuestion);
	mlt->addWidget(mergeDetail);
	mlt->addLayout(titleLt);
	mlt->addLayout(dateLt);
	mlt->addStretch(1);
	mlt->addLayout(clt);

	setLayout(mlt);
}

void listImportDlg::accept()
{
	if(answer->currentIndex() == 0){
		QSqlDatabase dbc= QSqlDatabase::database("CN");
		QSqlQuery qr(dbc);
		qr.prepare("SELECT DISTINCT pDate,pList FROM purchase WHERE pDate=:D AND pList=:L");
		qr.bindValue(":D", newDate->date().toString("yyyy-MM-dd"));
		qr.bindValue(":L", newTitle->text());
		qr.exec();
		if(qr.next()){
			QMessageBox::warning(this,tr("Warning"),tr("The list with title <b>'")
								 +newTitle->text()+tr("'</b> for  the date <b>'")
								 +newDate->date().toString("yyyy-MM-dd")
								 +tr("'</b> already exists!"),QMessageBox::Ok);
			return;
		}
	}
	QDialog::accept();
}

void listImportDlg::onAnswer(int idx)
{
	if(idx == 0){
		mergeQuestion->hide();
		mergeDetail->hide();

		newTitleL->show();
		newTitle->show();
		newDateL->show();
		newDate->show();
	}else if(idx == 1){
		mergeQuestion->show();
		mergeDetail->show();

		newTitleL->hide();
		newTitle->hide();
		newDateL->hide();
		newDate->hide();
	}else{
		mergeQuestion->hide();
		mergeDetail->hide();

		newTitleL->hide();
		newTitle->hide();
		newDateL->hide();
		newDate->hide();
	}
}

void listImportDlg::onMergeDetail(int idx){
	;
}

